package com.xiaoq.baiduunit.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import java.util.*;

/**
 * Created by Administrator on 2017/10/18.
 */
public class WeatherResult {

    private Integer error;
    private String status;
    private String date;
    private List<Results> results;

    @JsonIgnore
    private WeatherSuggest dressSuggest; //穿衣建议
    @JsonIgnore
    private WeatherSuggest washCarSuggest; //洗车建议
    @JsonIgnore
    private WeatherSuggest coldSuggest; //感冒建议
    @JsonIgnore
    private WeatherSuggest sportSuggest; //运动建议
    @JsonIgnore
    private WeatherSuggest ultravioletRaysSuggest; //紫外线建议

    @JsonIgnore
    private Map<String,LinkedHashSet<WeatherData>> weatherDataMap = new HashMap<>();

    @JsonIgnore
    private WeatherData todayWeatehr;

    public static class Results {
        private String currentCity;
        private String pm25;
        private List<WeatherSuggest> index;
        private List<WeatherData> weather_data;
		public String getCurrentCity() {
			return currentCity;
		}
		public void setCurrentCity(String currentCity) {
			this.currentCity = currentCity;
		}
		public String getPm25() {
			return pm25;
		}
		public void setPm25(String pm25) {
			this.pm25 = pm25;
		}
		public List<WeatherSuggest> getIndex() {
			return index;
		}
		public void setIndex(List<WeatherSuggest> index) {
			this.index = index;
		}
		public List<WeatherData> getWeather_data() {
			return weather_data;
		}
		public void setWeather_data(List<WeatherData> weather_data) {
			this.weather_data = weather_data;
		}
    }

    public static class WeatherSuggest {
        private String title;
        private String zs;
        private String tipt;
        private String des;
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public String getZs() {
			return zs;
		}
		public void setZs(String zs) {
			this.zs = zs;
		}
		public String getTipt() {
			return tipt;
		}
		public void setTipt(String tipt) {
			this.tipt = tipt;
		}
		public String getDes() {
			return des;
		}
		public void setDes(String des) {
			this.des = des;
		}
    }

    public static class WeatherData {
        private String date;
        private String dayPictureUrl;
        private String nightPictureUrl;
        private String weather;
        private String wind;
        private String temperature;
		public String getDate() {
			return date;
		}
		public void setDate(String date) {
			this.date = date;
		}
		public String getDayPictureUrl() {
			return dayPictureUrl;
		}
		public void setDayPictureUrl(String dayPictureUrl) {
			this.dayPictureUrl = dayPictureUrl;
		}
		public String getNightPictureUrl() {
			return nightPictureUrl;
		}
		public void setNightPictureUrl(String nightPictureUrl) {
			this.nightPictureUrl = nightPictureUrl;
		}
		public String getWeather() {
			return weather;
		}
		public void setWeather(String weather) {
			this.weather = weather;
		}
		public String getWind() {
			return wind;
		}
		public void setWind(String wind) {
			this.wind = wind;
		}
		public String getTemperature() {
			return temperature;
		}
		public void setTemperature(String temperature) {
			this.temperature = temperature;
		}
    }

    public Integer getError() {
        return error;
    }

    public String getStatus() {
        return status;
    }

    public String getDate() {
        return date;
    }

    public List<Results> getResults() {
        return results;
    }

    public WeatherSuggest getDressSuggest() {
        return dressSuggest;
    }

    public WeatherSuggest getWashCarSuggest() {
        return washCarSuggest;
    }

    public WeatherSuggest getColdSuggest() {
        return coldSuggest;
    }

    public WeatherSuggest getSportSuggest() {
        return sportSuggest;
    }

    public WeatherSuggest getUltravioletRaysSuggest() {
        return ultravioletRaysSuggest;
    }

    public void setError(Integer error) {
        this.error = error;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setDressSuggest(WeatherSuggest dressSuggest) {
        this.dressSuggest = dressSuggest;
    }

    public void setWashCarSuggest(WeatherSuggest washCarSuggest) {
        this.washCarSuggest = washCarSuggest;
    }

    public void setColdSuggest(WeatherSuggest coldSuggest) {
        this.coldSuggest = coldSuggest;
    }

    public void setSportSuggest(WeatherSuggest sportSuggest) {
        this.sportSuggest = sportSuggest;
    }

    public void setUltravioletRaysSuggest(WeatherSuggest ultravioletRaysSuggest) {
        this.ultravioletRaysSuggest = ultravioletRaysSuggest;
    }

    public void setResults(List<Results> results) {
        this.results = results;

        //解析天气建议
        this.parserSuggest();
        //解析天气
        this.parserWeatherData();
    }

    public WeatherData getTodayWeatehr() {
        return todayWeatehr;
    }

    private void parserSuggest(){
        if(results != null && results.size()>0){
            Results result = results.get(0);
            if(result != null && result.index!=null && result.index.size()>0){
                List<WeatherSuggest> weatherSuggestList = result.getIndex();
                int size = weatherSuggestList.size();
                if(size > 0) dressSuggest = weatherSuggestList.get(0);
                if(size > 1) washCarSuggest = weatherSuggestList.get(1);
                if(size > 2) coldSuggest = weatherSuggestList.get(2);
                if(size > 3) sportSuggest = weatherSuggestList.get(3);
                if(size > 4) ultravioletRaysSuggest = weatherSuggestList.get(4);
            }
        }
    }

    private void parserWeatherData(){
        if(results != null && results.size()>0){
            Results result = results.get(0);
            if(result != null && result.weather_data!=null && result.weather_data.size()>0){
                List<WeatherData> weatherDataList = result.getWeather_data();
                String[][] weatherDateArray = WeatherDateKey.dateArray(new DateTime());
                int size = weatherDataList.size();
                for (int i = 0 ; i<size; i++){
                    WeatherData weatherData = weatherDataList.get(i);

                    if(i == 0){
                        todayWeatehr = weatherData;
                    }

                    String[] temp = weatherDateArray[i];
                    for(String str : temp){
                        if(weatherDataMap.containsKey(str)){
                            weatherDataMap.get(str).add(weatherData);
                        }else {
                            LinkedHashSet set = new LinkedHashSet();
                            set.add(weatherData);
                            weatherDataMap.put(str, set);
                        }
                    }
                }
            }
        }
    }

    /***
     *  dateKey 为WeatherKey中的数据
     * @param dateKey
     * @return
     */
    public boolean containWeatherDataByKey(String dateKey){
        if(StringUtils.isNotBlank(dateKey)){
            return weatherDataMap.containsKey(dateKey);
        }
        return false;
    }

    /**
     * set 为有序
     * @return
     */
    public Set<WeatherData> getWeatherDataSet(String dateKey){
        if(containWeatherDataByKey(dateKey) ){
            return weatherDataMap.get(dateKey);
        }
        return null;
    }

    public Map<String, LinkedHashSet<WeatherData>> getWeatherDataMap() {
        return weatherDataMap;
    }

    @Override
    public String toString() {
        return "WeatherResult{" +
                "error=" + error +
                ", status='" + status + '\'' +
                ", date='" + date + '\'' +
                ", results=" + results +
                ", dressSuggest=" + dressSuggest +
                ", washCarSuggest=" + washCarSuggest +
                ", coldSuggest=" + coldSuggest +
                ", sportSuggest=" + sportSuggest +
                ", ultravioletRaysSuggest=" + ultravioletRaysSuggest +
                ", weatherDataMap=" + weatherDataMap +
                '}';
    }

    public void setWeatherDataMap(Map<String, LinkedHashSet<WeatherData>> weatherDataMap) {
        this.weatherDataMap = weatherDataMap;
    }

    public void setTodayWeatehr(WeatherData todayWeatehr) {
        this.todayWeatehr = todayWeatehr;
    }
}

